$(document).ready(function(){
	menuActionUser();
});

function menuActionUser(){
	jQuery('li.view-permissions a.function').click(function(){loadPermissions();});
	jQuery('li.set-permissions a.function').click(function(){loadSetPermissions(null);});
	jQuery('li.add-permissions a.function').click(function(){loadAddPermissions();});
	
	jQuery('li.set-user a.function').click(function(){loadSetUser(idUser);});
	jQuery('li.add-user a.function').click(function(){loadAddUser();});
}


function loadPermissions(){
	$('#admin-top-loader').show();
	jQuery.ajax({
		url:'/ajax/core/admin/getPermissionsUser.php',
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				putHtml(json.html, jQuery('div.admin-col-l'));
			} else {
				alert(json.error);
			}
		}
	});
}

function loadSetPermissions(idu){
	if (idu == null) {
		idu = idUser;
	}
	
	$('#admin-top-loader').show();
	jQuery.ajax({
		url:'/ajax/core/admin/getPermissions.php',
		type:'POST',
		data: {'idu': idu},
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				putHtml(json.html, jQuery('div.admin-col-l'));
				changeUser4SetPermission();
				clickEvent4SetPermission();
			} else {
				alert(json.error);
			}
		}
	});
}

function changeUser4SetPermission(){
	$('select[name="user"]').change(function(){
		var idu = $(this).val();
		loadSetPermissions(idu);
	});
}

function changeUser4SetDettails(){
	$('select[name="user"]').change(function(){
		var idu = $(this).val();
		loadSetUser(idu);
	});
}

function clickEvent4SetPermission(){
	var img = $('li.row-permission-set').children('div').children('img');
	
	img.click(function (){
		var data = $(this).attr('data');
		var dataObj = eval('(' + data + ')');
		setPermission(dataObj.idu, dataObj.idp, dataObj.add);
	});
}

function setPermission(idu, idp, add){
	$('#admin-top-loader').show();	
	jQuery.ajax({
		url:'/ajax/core/admin/setPermission.php',
		type:'POST',
		data:{'idu':idu, 'idp':idp, 'add': add},
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				loadSetPermissions(idu);
			} else {
				alert(json.error);
			}
		}
	});
}

function loadAddPermissions() {
	$('#admin-top-loader').show();
	jQuery.ajax({
		url:'/ajax/core/admin/addPermission.php',
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				putHtml(json.html, jQuery('div.admin-col-l'));
				eventAjaxSendForm(jQuery('#admin-add-permission'), '/ajax/core/admin/createPermission.php');
			} else {
				alert(json.error);
			}
		}
	});
}

function loadSetUser(idUser) {
	$('#admin-top-loader').show();
	jQuery.ajax({
		url:'/ajax/core/admin/getUser.php',
		type:'POST',
		data:{'idu': idUser},
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				putHtml(json.html, jQuery('div.admin-col-l'));
				changeUser4SetDettails();
				eventAjaxSendForm(jQuery('#admin-set-user'), '/ajax/core/admin/setUser.php');
				eventAjaxSendForm(jQuery('#admin-set-user-pass'), '/ajax/core/admin/setPassword.php');
			} else {
				alert(json.error);
			}
		}
	});
}

function loadAddUser() {
	$('#admin-top-loader').show();
	jQuery.ajax({
		url:'/ajax/core/admin/addUser.php',
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				putHtml(json.html, jQuery('div.admin-col-l'));				
				eventAjaxSendForm(jQuery('#admin-set-user'), '/ajax/core/admin/setUser.php');
			} else {
				alert(json.error);
			}
		}
	});
}